/**
 * Copyright 2007 American Well Systems
 * All rights reserved.
 *
 * It is illegal to use, reproduce or distribute
 * any part of this Intellectual Property without
 * prior written authorization from American Well.
 */
package org.rich.charlesmurphy.core;

import static org.rich.charlesmurphy.core.CMIds.PATH;
import static org.rich.charlesmurphy.core.CMIds.URL;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ddevil.data.Data;
import org.ddevil.data.Filter;
import org.ddevil.data.set.DataSet;
import org.ddevil.data.util.DataSetUtils;
import org.rich.charlesmurphy.core.filters.DataRegexFilter;
import org.rich.charlesmurphy.core.filters.HostnameFilter;
import org.rich.charlesmurphy.model.CharlieMurphyModel;
import org.rich.charlesmurphy.model.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

/**
 * @author Rich O'Connell
 */
public class DataLoader {

	public static final String PROP_HOST = "HOSTNAME";
	public static final String PROP_FILTER_AJAX = "FILTER_AJAX";

	private Logger log = LoggerFactory.getLogger(getClass());

	private Map<InputFileType, InputProcessor> processors = new HashMap<InputFileType, InputProcessor>();

	private static class InputProcessor {
		Parser parser;
		Transformer transformer;
		private InputProcessor(Parser parser, Transformer transformer) {
			this.parser = parser;
			this.transformer = transformer;
		}
	}

	public void addInputProcessor(InputFileType fileType, Parser parser, Transformer transformer) {
		processors.put(fileType, new InputProcessor(parser, transformer));
	}

	public CharlieMurphyModel loadData(DataLoaderCommand command) {
		InputProcessor processor = processors.get(command.getFileType());
		Parser parser = processor.parser;
		Transformer xformer = processor.transformer;

		DataSet<Request> importData = null;

		for (File file : command.getFiles()) {

			ParseResult results = parser.parse(file);

			List<Filter<Data>> filters = new ArrayList<Filter<Data>>(2);
			filters.add(new DataRegexFilter(PATH, ".*heartbeat\\.ajax.*", true, true));
			filters.add(new DataRegexFilter(PATH, ".*getStatus\\.ajax.*", true, true));
			filters.add(new DataRegexFilter(PATH, "/ws", true, true));
			filters.add(new DataRegexFilter(PATH, ".*/bea_wls_internal.*", true, true)); //weblogic internal



			String hostname = command.getHost();
			if (StringUtils.hasText(hostname)) {
				filters.add(new HostnameFilter(URL, hostname));

			}

			DataSet<Request> xFormedData = xformer.transform(results.getDataSet());


			log.debug("Done transform, [{}] records.", xFormedData.size());
			for (Filter<Data> f : filters) {
				DataSetUtils.filterDataSet(xFormedData, f);
				log.debug("Ran filter: {}, left with: [{}] records.", f.getClass().getSimpleName(), xFormedData.size());
			}

			if (importData == null) {
				importData = xFormedData;
			}
			else {
				importData.importRecords(xFormedData);
			}
		}

		return new CharlieMurphyModel(importData, command.isFilterAjax());
	}

}
